package com.xiaocz.hblsubstitutedriving.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.common.widgets.load.LoadFactory;
import com.xiaocz.common.widgets.load.core.LoadManager;
import com.xiaocz.common.widgets.load.view.AbsView;
import com.xiaocz.common.widgets.recycler.RecyclerViewAdapter;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.adapter.WithdrawDepositListAdapter;
import com.xiaocz.hblsubstitutedriving.base.BaseActivity;
import com.xiaocz.hblsubstitutedriving.model.Account;
import com.xiaocz.hblsubstitutedriving.model.WithDrawDeposit;
import com.xiaocz.hblsubstitutedriving.widget.EmptyView;
import com.xiaocz.hblsubstitutedriving.widget.ErrorView;
import com.xiaocz.hblsubstitutedriving.widget.LoadingView;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Time:2019/7/23
 * Created by xiaocz
 * Describe 提现记录
 */
public class TiXianListActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    public RecyclerView mRecyclerView;

    /**
     * DATA
     */
    private LoadManager mLoadManager;
    private WithdrawDepositListAdapter withdrawDepositListAdapter;

    public static void show(Context context) {
        Intent intent = new Intent(context, TiXianListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_list;
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        mLoadManager = LoadFactory.getInstance().register(refreshLayout, new AbsView.OnReloadListener() {
            @Override
            public void onReload(View view) {
                mLoadManager.showStateView(LoadingView.class);
                webMyOrder();
            }
        });
        setRefresh();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && resultCode == 2) {
            webMyOrder();
        }
    }

    private int page = 1;

    private void setRefresh() {
        // 下拉刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh();
                webMyOrder();
            }
        });
        //加载更多
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore();
                page++;
                webMyOrder();
            }
        });
        webMyOrder();
    }

    @Override
    protected void initData() {
        super.initData();
        tvTitle.setText("提现记录");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        withdrawDepositListAdapter = new WithdrawDepositListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<WithDrawDeposit>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<WithDrawDeposit> holder, WithDrawDeposit data) {
                super.onItemClick(holder, data);
                WithdrawDepositInfoActivity.show(mContext, data.getOrderNo());
            }
        });
        mRecyclerView.setAdapter(withdrawDepositListAdapter);
        webMyOrder();
    }

    /**
     * 我的提现记录
     */
    private void webMyOrder() {
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", AccountManagers.getUserId());
        asyncHttpPostFormData.addFormData("PageIndex", page);
        asyncHttpPostFormData.addFormData("PageSize", Common.Constant.pageSize);
        okHttpClient.post(Common.Constant.API_IP + "api/TiXian/QueryDriverTiXianOrderList", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                mLoadManager.showStateView(ErrorView.class);
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                Type type = new TypeToken<RspModel<Account>>() {
                }.getType();
                RspModel<Account> rspModel = getAnalysisBase(mLoadManager, response, type, "我的提现记录");
                if (rspModel == null) {
                    mLoadManager.showStateView(EmptyView.class);
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.isStatus() && rspModel.getData() != null) {
                            Account account = rspModel.getData();
                            if(!"[]".equals(account.getTixianlist().toString())){
                                if (page == 1) {
                                    withdrawDepositListAdapter.replace(account.getTixianlist());
                                } else {
                                    withdrawDepositListAdapter.add(account.getTixianlist());
                                }
                            }else {
                                App.getInstance().showMessage(getResources().getString(R.string.none_error));
                                mLoadManager.showStateView(EmptyView.class);
                            }
                        } else {
                            if (page == 1) {
                                mLoadManager.showStateView(EmptyView.class);
                            } else {
                                App.getInstance().showMessage(rspModel.getMessage());
                            }
                        }
                        break;
                    default:
                        mLoadManager.showStateView(ErrorView.class);
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }
}
